﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml;

namespace CarPCBATest.Procedure
{
    [Serializable]
    public class STD_IN
    {
        public string EDI_user { get; set; } = "MESUSER";
        public string EDI_pwd { get; set; } = "MESPWD";
        public string ObjectID { get; set; } = "ic_fct_record";
        public Service Service { get; set; } = new Service();

        public void Update(string PROSN, string PQCSTATION, string PQCOPERATOR, bool isOk, string PQCREMARK = null, string[] ngMsg = null)
        {
            Service.Update(PROSN, PQCSTATION, PQCOPERATOR, isOk, PQCREMARK, ngMsg);
        }

        public string GenerateStringXml()
        {
            // 使用 XmlSerializer 将对象转换为 XML 字符串
            XmlSerializer serializer = new XmlSerializer(typeof(STD_IN));

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Encoding = new UTF8Encoding(false), // 使用 false 避免写入 BOM
                Indent = true
            };
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings))
                {
                    serializer.Serialize(xmlWriter, this);
                }

                // 获取 UTF-8 编码的 XML 字符串
                string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());
                return xmlString;
            }
        }

        public (string, string) ParseReturnXml(string xmlString)
        {
            string[] statusArray = xmlString.Split(new string[] { "<Status>", "</Status>" }, StringSplitOptions.RemoveEmptyEntries);

            string[] errorArror = xmlString.Split(new string[] { "<Error>", "</Error>" }, StringSplitOptions.RemoveEmptyEntries);
            return (statusArray[1], errorArror[1]);
        }
    }

    [Serializable]
    public class Service
    {
        public PQC PQC { get; set; } = new PQC();

        public void Update(string PROSN, string PQCSTATION, string PQCOPERATOR, bool isOk, string PQCREMARK = null, string[] ngMsg = null)
        {
            PQC.Update(PROSN, PQCSTATION, PQCOPERATOR, isOk, PQCREMARK, ngMsg);
        }
    }

    [Serializable]
    public class PQC
    {
        public void Update(string PROSN, string PQCSTATION, string PQCOPERATOR, bool isOk, string PQCREMARK = null, string[] ngMsg = null)
        {
            this.PROSN = PROSN;
            this.PQCSTATION = PQCSTATION;
            this.PQCREMARK = PQCREMARK;
            this.PQCTESTDT = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (isOk)
            {
                PQCQUALSTATE = "0";
                ERR_ITEMS = null;
            }
            else
            {
                PQCQUALSTATE = "1";
                ERR_ITEMS = new List<ERR_ITEM>();
                foreach (var item in ngMsg)
                {
                    ERR_ITEMS.Add(new ERR_ITEM { ERRCODE = "FCN_NG", ERRNAME = item });
                }
            }
        }

        public string PROSN { get; set; } = "P00187972H0026S000389F1040200000006";
        public string PROSNS { get; set; } = "产品SN/小板SN";
        public string PROWONO { get; set; } = "工单号";

        public string PQCSTATION { get; set; } = "HYS-FCT03";
        public string PQCOPERATOR { get; set; } = "TestOperator01";
        public string PQCQUALSTATE { get; set; } = "0";
        public string PQCTESTDT { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public string PQCREMARK { get; set; } = "";

        public List<ERR_ITEM> ERR_ITEMS { get; set; } = null;
    }

    [Serializable]
    public class ERR_ITEM
    {
        public string ERRCODE { get; set; } = "FCT_NG";
        public string ERRNAME { get; set; } = "";
    }
}